package com.hundsun.yr.universal.p2p.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.adapter.HolderAdapter;
import com.hundsun.yr.universal.library.adapter.ViewCreatorHelper;
import com.hundsun.yr.universal.library.adapter.ViewCreatorHelper.UpdateItem;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.utils.SpannableStringBuilderUtils;
import com.hundsun.yr.universal.library.view.hsdialog.HSDialog;
import com.hundsun.yr.universal.library.view.hsdialog.HSDialog.HSDialogOnItemClickListener;
import com.hundsun.yr.universal.library.widget.HSButton;
import com.hundsun.yr.universal.library.widget.HSLoadMoreListView;
import com.hundsun.yr.universal.library.widget.HSLoadMoreListView.OnLoadMoreListener;
import com.hundsun.yr.universal.library.widget.HSSwipeRefreshLayout;
import com.hundsun.yr.universal.p2p.bean.MyLoanListBeen;
import com.hundsun.yr.universal.p2p.bean.TradeRecord;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.config.Constants;
import com.hundsun.yr.universal.p2p.config.Extras;
import com.hundsun.yr.universal.p2p.config.TaskConstants;
import com.hundsun.yr.universal.p2p.presenter.base.RequestPresenter;
import com.hundsun.yr.universal.p2p.presenter.impl.WaitPayListPresenterImpl;
import com.hundsun.yr.universal.p2p.ui.activity.LoanDetailActivity;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseFragment;
import com.hundsun.yr.universal.p2p.utils.BaseRequestTool2;
import com.hundsun.yr.universal.p2p.utils.CommTool;
import com.hundsun.yr.universal.p2p.view.base.BaseListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


/**
 * @version V1.0
 * @Title: ApplyInFragment.java
 * @Description: 我的借款-申请中-撤销
 * @date 2016-1-5 下午2:51:14
 * @author:hejun.shen
 * @email:shenhj15971@hundsun.com
 * @replace author:
 * @replace date:
 */
public class ApplyingFragment extends BaseFragment implements BaseListView,
        OnRefreshListener, OnItemClickListener, OnLoadMoreListener, UpdateItem<MyLoanListBeen>, HSDialogOnItemClickListener {

    /**
     * 适配器
     **/
    private HolderAdapter<MyLoanListBeen> mAdapter = null;
    /**
     * 上拉列表（listView）
     **/
    private HSLoadMoreListView lv_waitpay = null;
    /**
     * 下拉容器
     **/
    private HSSwipeRefreshLayout hsSwipeRefreshLayout = null;
    /**
     * 索引偏移量
     **/
    private int start = 0;
    /**
     * 数据集合
     **/
    private List<MyLoanListBeen> data = new ArrayList<MyLoanListBeen>();
    /**
     * 请求接口
     **/
    private RequestPresenter mPresenter = null;

    /**
     * 支付持续时间
     **/
    private String outTime;
    /**
     * 选中索引
     **/
    @SuppressWarnings("unused")
    private int mPosition;
    /**
     * 系统时间
     **/
    private String zwgt_data;

    /**
     * 撤销的uuid
     */
    private String uuid;
    /**
     * dialog申请取消
     */
    private HSDialog dialog_applycancel = null;
    private int lastVisiblePosition;
    private Long time;
    private LayoutInflater mInflater;
    private RelativeLayout headerView;

    @Override
    protected void onFirstUserVisible() {
        // TODO Auto-generated method stub
        loadingData();
    }

    @Override
    protected void onUserVisible() {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onUserInvisible() {
        // TODO Auto-generated method stub

    }

    @Override
    protected View getLoadingTargetView() {
        // TODO Auto-generated method stub
        hsSwipeRefreshLayout = findView(R.id.hsSwipeRefreshLayout_applyin2);
        return hsSwipeRefreshLayout;
    }

    @Override
    protected void initViewsAndEvents() {
        // TODO Auto-generated method stub
        if (mPresenter == null) {
            mPresenter = new WaitPayListPresenterImpl(mContext, this, TradeRecord.BASEKEY);
        }
        if (hsSwipeRefreshLayout == null) {
            hsSwipeRefreshLayout = (HSSwipeRefreshLayout) getActivity().findViewById(R.id.hsSwipeRefreshLayout_applyin2);
        }

        lv_waitpay = findView(R.id.lv_applyin2);

        ViewCreatorHelper<MyLoanListBeen> creatorHelper = new ViewCreatorHelper<MyLoanListBeen>(R.layout.item_my_loan_all, this);
        mAdapter = new HolderAdapter<MyLoanListBeen>(mContext, creatorHelper.viewCreator());

        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        headerView = (RelativeLayout) mInflater.inflate(R.layout.item_header_10dp, lv_waitpay, false);
        lv_waitpay.addHeaderView(headerView);

        lv_waitpay.setAdapter(mAdapter);

        hsSwipeRefreshLayout.setOnRefreshListener(this);
        lv_waitpay.setOnLoadMoreListener(this);

        lv_waitpay.setOnItemClickListener(this);
    }

    /**
     * 加载数据
     */
    private void loadingData() {
        hsSwipeRefreshLayout.setRefreshing(true);
        mPresenter = new WaitPayListPresenterImpl(mContext, this, TradeRecord.BASEKEY);
        zwgt_data = BaseRequestTool2.getFormatedDateString();
        mPresenter.loadingData(TaskConstants.LOAN_LIST, getString(R.string.root_url_test) + getString(R.string.api_user_test) + TaskConstants.LOAN_LIST + "?type=" + 2,
                Constants.EVENT_REFRESH_DATA, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_GET, getString(R.string.api_user_test) + TaskConstants.LOAN_LIST + "?type=" + 2,
                        sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data), true);
    }

    @Override
    protected int getContentViewLayoutID() {
        // TODO Auto-generated method stub
        return R.layout.frag_applying;
    }

    @Override
    protected void onEveComming(@SuppressWarnings("rawtypes") EventCenter eventCenter) {
        if (eventCenter.getEventCode() == Extras.LOAN_ORDER_SUCCESS){
            loadingData();
        }
    }

    @Override
    protected boolean isBindEventBusHere() {
        // TODO Auto-generated method stub
        return true;
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T> void refreshListData(List<T> result) {
        hsSwipeRefreshLayout.setRefreshing(false);
        lv_waitpay.setLoadMoreEnabled(true);
        if(data.size() > 0) data.clear();
        if(result!=null){
            data.addAll((Collection<? extends MyLoanListBeen>) result);
        }
        mAdapter.update(data);
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T> void addMoreListData(List<T> result) {
        // TODO Auto-generated method stub
        lv_waitpay.onLoadMoreComplete();

        if(result!=null && result.size() > 0){
            data.addAll((Collection<? extends MyLoanListBeen>) result);
            lv_waitpay.setLoadMoreEnabled(true);
        } else {
            lv_waitpay.setLoadMoreEnabled(false);
        }
        mAdapter.update(data);
    }

    @Override
    public <T> void onFailure(String task_name) {
        // TODO Auto-generated method stub
        hsSwipeRefreshLayout.setRefreshing(false);
        lv_waitpay.onLoadMoreComplete();
    }

    @Override
    public void onRefresh() {
        // TODO Auto-generated method stub
        start = 0;
        zwgt_data = BaseRequestTool2.getFormatedDateString();
        mPresenter.loadingData(TaskConstants.LOAN_LIST, getString(R.string.root_url_test) + getString(R.string.api_user_test) + TaskConstants.LOAN_LIST + "?type=" + 2,
                Constants.EVENT_REFRESH_DATA, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_GET, getString(R.string.api_user_test) + TaskConstants.LOAN_LIST + "?type=" + 2,
                        sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data), false);
    }

    @Override
    public void onLoadMore() {
        // TODO Auto-generated method stub
        zwgt_data = BaseRequestTool2.getFormatedDateString();

        lastVisiblePosition = lv_waitpay.getLastVisiblePosition();
        time = Long.valueOf(data.get(lastVisiblePosition - 2).ctime);

        mPresenter.loadingData(TaskConstants.LOAN_LIST, getString(R.string.root_url_test) + getString(R.string.api_user_test) + TaskConstants.LOAN_LIST + "?type=" + 2 + "&ctime=" + time,
                Constants.EVENT_LOAD_MORE_DATA, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_GET, getString(R.string.api_user_test) + TaskConstants.LOAN_LIST + "?type=" + 2 + "&ctime=" + time,
                        sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data), false);
    }

    @Override
    public void refreshItem(View view, int position, final MyLoanListBeen invest) {
        /**还款利率**/
        TextView tv_annualincome = findView(view, R.id.tv_annualincome);
        SpannableStringBuilder spaBuilder;
        if (invest.debt_rate != null && !invest.debt_rate.isEmpty()){
            spaBuilder = (SpannableStringBuilderUtils.sizeAndColorSpan
                    (CommTool.keep1Digits(invest.debt_rate) +  "%", "%", 14, mContext.getResources().getColor(R.color.cl_redformula_text_3837), true));
            tv_annualincome.setText(spaBuilder);
        } else {
            spaBuilder = (SpannableStringBuilderUtils.sizeAndColorSpan
                    ("0" +  "%", "%", 14, mContext.getResources().getColor(R.color.cl_redformula_text_3837), true));
            tv_annualincome.setText(spaBuilder);
        }

        /**营销商品名称**/
        TextView tv_title = findView(view, R.id.tv_title);
        if (invest.prdname != null && !invest.prdname.isEmpty()) {
            tv_title.setText(invest.prdname);
        } else {
            tv_title.setText("申请编号" + invest.uuid);
        }

        /**募集总额|申请金额**/
        TextView tv_income = findView(view, R.id.tv_income);
        tv_income.setText(SpannableStringBuilderUtils.sizeSpan(getString(R.string._yuan,CommTool.keep2Digits(invest.amount)), getString(R.string.yuan), 12));

        /**募集状态**/
        TextView tv_prd_progress = findView(view, R.id.tv_prd_progress);
        tv_prd_progress.setText(invest.getPrdprogress());

        /**还款方式  右下**/
        TextView tv_incomemonet = findView(view, R.id.tv_incomemonet);
        tv_incomemonet.setText(invest.getPayMethod(mContext));

        /**下一期还款额度  右下**/
        TextView tv_next_amount = findView(view, R.id.tv_next_amount);
        SpannableStringBuilder spaBuilder_tv_next_amount;

        /**撤销按钮  右中**/
        HSButton btn_cancel = findView(view, R.id.btn_prd_progress);

        /**募集总额|申请金额  中**/
        TextView tv_raise_amount = findView(view, R.id.tv_raise_amount);

        /**还款进度|借款期限  左下**/
        TextView tv_invset_money = findView(view, R.id.tv_invset_money);
        SpannableStringBuilder spaBuilder_tv_invset_money;

        /**下期还款  右下  偏上**/
        TextView tv_next_term = findView(view, R.id.tv_next_term);

        btn_cancel.setVisibility(View.GONE);
        /**
         * 撤销点击事件
         */
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                uuid = invest.uuid;
                dialog_applycancel = new HSDialog(mContext.getString(R.string.tips),mContext.getString(R.string.tips_revoke_apply),mContext.getString(R.string.cancel), mContext.getString(R.string.sure),mContext,ApplyingFragment.this).setCancelable(false);//.setOnDismissListener(this);
                dialog_applycancel.show();
            }
        });
        /**
         * 0	用户草稿
         1	一审（风控）审批中
         2	二审（运营）审批中
         10	审批完成，已转产品。
         100	用户撤回
         110	一审（风控）拒绝
         120	二审（运营）拒绝
         */
        if (TextUtils.equals(invest.applyprogress, "0")) {
            //用户草稿  显示：还款方式，撤销按钮，申请金额，借款期限    ；隐藏：下期还款金额，募集状态，募集总额，还款进度，下期还款日
            /**设置还款方式**/
            tv_incomemonet.setVisibility(View.VISIBLE);
            /**下期返款金额**/
            tv_next_amount.setVisibility(View.GONE);
            /**募集状态**/
            tv_prd_progress.setVisibility(View.GONE);
            /**撤销按钮**/
            btn_cancel.setVisibility(View.VISIBLE);
            /**申请金额|募集总额**/
            tv_raise_amount.setText(getString(R.string._raise_money));
            /**还款进度|借款期限**/
            spaBuilder_tv_invset_money = (SpannableStringBuilderUtils.colorSpan
                    (getString(R.string.loan_term, invest.getTerm()), invest.getTerm(), mContext.getResources().getColor(R.color.cl_redformula_text_3837)));
            tv_invset_money.setText(spaBuilder_tv_invset_money);
            /**下期还款日**/
            tv_next_term.setVisibility(View.GONE);
        } else if (TextUtils.equals(invest.applyprogress, "1")) {
            //风控审批中 显示还款方式，撤销按钮，申请金额，借款期限    ；隐藏：下期还款金额，募集状态，募集总额，还款进度，下期还款日
            /**设置还款方式**/
            tv_incomemonet.setVisibility(View.VISIBLE);
//            tv_incomemonet.setText(invest.getPayMethod(mContext));
            /**下期返款金额**/
            tv_next_amount.setVisibility(View.GONE);
            /**募集状态**/
            tv_prd_progress.setVisibility(View.GONE);
            /**撤销按钮**/
            btn_cancel.setVisibility(View.VISIBLE);
            /**申请金额|募集总额**/
            tv_raise_amount.setText(getString(R.string._raise_money));
            /**还款进度|借款期限**/
            spaBuilder_tv_invset_money = (SpannableStringBuilderUtils.colorSpan
                    (getString(R.string.loan_term, invest.getTerm()), invest.getTerm(), mContext.getResources().getColor(R.color.cl_redformula_text_3837)));
            tv_invset_money.setText(spaBuilder_tv_invset_money);
            /**下期还款日**/
            tv_next_term.setVisibility(View.GONE);
        } else if (TextUtils.equals(invest.applyprogress, "2")) {
            //运营审批中  显示还款方式，申请金额，借款期限    ；隐藏：下期还款金额，募集状态，撤销按钮，募集总额，还款进度，下期还款日
            /**设置还款方式**/
            tv_incomemonet.setVisibility(View.VISIBLE);
//            tv_incomemonet.setText(invest.getPayMethod(mContext));
            /**下期返款金额**/
            tv_next_amount.setVisibility(View.GONE);
            /**募集状态**/
            tv_prd_progress.setVisibility(View.GONE);
            /**撤销按钮**/
            btn_cancel.setVisibility(View.VISIBLE);
            /**申请金额|募集总额**/
            tv_raise_amount.setText(getString(R.string._raise_money));
            /**还款进度|借款期限**/
            spaBuilder_tv_invset_money = (SpannableStringBuilderUtils.colorSpan
                    (getString(R.string.loan_term, invest.getTerm()), invest.getTerm(), mContext.getResources().getColor(R.color.cl_redformula_text_3837)));
            tv_invset_money.setText(spaBuilder_tv_invset_money);
            /**下期还款日**/
            tv_next_term.setVisibility(View.GONE);
        } else if (TextUtils.equals(invest.applyprogress, "10")) {
            //审批完成，已经转为产品  显示还款进度，募集总额，显示还款状态，下期还款时间，下期还款金额；   隐藏：还款方式，撤销按钮，申请金额，借款期限
            /**设置还款方式**/
            tv_incomemonet.setVisibility(View.GONE);
            /**下期返款金额**/
            tv_next_amount.setVisibility(View.VISIBLE);
            spaBuilder_tv_next_amount = (SpannableStringBuilderUtils.colorSpan
                    (getString(R.string.next_term_repayment,invest.next_repay_amt), invest.next_repay_amt, mContext.getResources().getColor(R.color.cl_redformula_text_3837)));
            tv_next_amount.setText(spaBuilder_tv_next_amount);

            /**下期还款日**/
            tv_next_term.setVisibility(View.VISIBLE);
            tv_next_term.setText(getString(R.string.next_term,invest.next_repay_day));

            /**募集状态**/
            tv_prd_progress.setVisibility(View.VISIBLE);
            //根据不同的募集状态，对ui进行相应的改变]

            /**  50和小于20的
             * 0	未发布	0：未发布
             10	投标中	1：投标中
             15	待放款	无
             18	放款中	无
             20	计息中	2：计息中
             30	待返还（暂时不用）	3：待返还
             40	已返还	4：已返还
             50	已结束	5：已结束
             60	已撤销	6：已结束
             100	逾期未返还
             105	逾期部分返还（暂不开发）
             110	逾期已返还
             120	流标
             125	停售（募集期截止但未募满）
             130	风控拒绝放款
             200	逾期已返还(与11重复废弃)
             */
            if (TextUtils.equals(invest.prdprogress,"0")){
                tv_next_amount.setVisibility(View.GONE);
                tv_next_term.setVisibility(View.GONE);
                tv_incomemonet.setVisibility(View.VISIBLE);
                spaBuilder_tv_invset_money = (SpannableStringBuilderUtils.colorSpan
                        (getString(R.string.loan_term, invest.getTerm()), invest.getTerm(), mContext.getResources().getColor(R.color.cl_redformula_text_3837)));
                tv_invset_money.setText(spaBuilder_tv_invset_money);
            } else if (TextUtils.equals(invest.prdprogress,"10")){
                tv_next_amount.setVisibility(View.GONE);
                tv_next_term.setVisibility(View.GONE);
                tv_incomemonet.setVisibility(View.VISIBLE);
                spaBuilder_tv_invset_money = (SpannableStringBuilderUtils.colorSpan
                        (getString(R.string.loan_term, invest.getTerm()), invest.getTerm(), mContext.getResources().getColor(R.color.cl_redformula_text_3837)));
                tv_invset_money.setText(spaBuilder_tv_invset_money);
            } else if (TextUtils.equals(invest.prdprogress,"15")){
                //待放款
                tv_next_amount.setVisibility(View.GONE);
                tv_next_term.setVisibility(View.GONE);
                tv_incomemonet.setVisibility(View.VISIBLE);
                spaBuilder_tv_invset_money = (SpannableStringBuilderUtils.colorSpan
                        (getString(R.string.loan_term, invest.getTerm()), invest.getTerm(), mContext.getResources().getColor(R.color.cl_redformula_text_3837)));
                tv_invset_money.setText(spaBuilder_tv_invset_money);
//                tv_incomemonet.setText(invest.getPayMethod(mContext));
            } else if (TextUtils.equals(invest.prdprogress,"18")){
                // TODO: 2017/5/26 放款中显示的ui
                tv_next_amount.setVisibility(View.GONE);
                tv_next_term.setVisibility(View.GONE);
                tv_incomemonet.setVisibility(View.VISIBLE);
                spaBuilder_tv_invset_money = (SpannableStringBuilderUtils.colorSpan
                        (getString(R.string.loan_term, invest.getTerm()), invest.getTerm(), mContext.getResources().getColor(R.color.cl_redformula_text_3837)));
                tv_invset_money.setText(spaBuilder_tv_invset_money);
            } else if (TextUtils.equals(invest.prdprogress,"20")){
                // 计息中--待还款
                tv_next_amount.setVisibility(View.VISIBLE);
                spaBuilder_tv_next_amount = (SpannableStringBuilderUtils.colorSpan
                        (getString(R.string.next_term_repayment,invest.next_repay_amt), invest.next_repay_amt, mContext.getResources().getColor(R.color.cl_redformula_text_3837)));
                tv_next_amount.setText(spaBuilder_tv_next_amount);
                tv_next_term.setVisibility(View.VISIBLE);
                tv_next_term.setText(getString(R.string.next_term,invest.next_repay_day));
                tv_incomemonet.setVisibility(View.GONE);
                spaBuilder_tv_invset_money = (SpannableStringBuilderUtils.colorSpan
                        (getString(R.string.repayment_schedule, invest.repayed_term + "/" + invest.retotal_term), invest.repayed_term + "/" + invest.retotal_term, mContext.getResources().getColor(R.color.cl_redformula_text_3837)));
                tv_invset_money.setText(spaBuilder_tv_invset_money);
            } else if (TextUtils.equals(invest.prdprogress,"30")){

            } else if (TextUtils.equals(invest.prdprogress,"40")){

            } else if (TextUtils.equals(invest.prdprogress,"50")){
                //已结束
                tv_next_amount.setVisibility(View.GONE);
                tv_next_term.setVisibility(View.GONE);
                tv_incomemonet.setVisibility(View.VISIBLE);
                spaBuilder_tv_invset_money = (SpannableStringBuilderUtils.colorSpan
                        (getString(R.string.repayment_schedule, invest.repayed_term + "/" + invest.retotal_term), invest.repayed_term + "/" + invest.retotal_term, mContext.getResources().getColor(R.color.cl_redformula_text_3837)));
                tv_invset_money.setText(spaBuilder_tv_invset_money);
//                tv_incomemonet.setText(invest.getPayMethod(mContext));
            } else if (TextUtils.equals(invest.prdprogress,"60")){

            } else if (TextUtils.equals(invest.prdprogress,"100")){

            } else if (TextUtils.equals(invest.prdprogress,"105")){

            } else if (TextUtils.equals(invest.prdprogress,"120")){

            } else if (TextUtils.equals(invest.prdprogress,"125")){

            } else if (TextUtils.equals(invest.prdprogress,"130")){

            } else if (TextUtils.equals(invest.prdprogress,"200")){

            } else {
            }
            /**撤销按钮**/
            btn_cancel.setVisibility(View.GONE);
            /**申请金额|募集总额**/
            tv_raise_amount.setText(getString(R.string._raise_totle_amount));
            /**还款进度|借款期限**/

        } else if (TextUtils.equals(invest.applyprogress, "100")) {
            tv_next_amount.setVisibility(View.GONE);
            tv_next_term.setVisibility(View.GONE);
            tv_incomemonet.setVisibility(View.VISIBLE);

            /**募集状态**/
            tv_prd_progress.setVisibility(View.VISIBLE);
            tv_prd_progress.setText("已撤回");

            //用户撤回--不会有这种状态返回
            /**撤销按钮**/
            btn_cancel.setVisibility(View.GONE);
            /**申请金额|募集总额**/
            tv_raise_amount.setText(getString(R.string._raise_money));
            /**还款进度|借款期限**/
            spaBuilder_tv_invset_money = (SpannableStringBuilderUtils.colorSpan
                    (getString(R.string.loan_term, invest.getTerm()), invest.getTerm(), mContext.getResources().getColor(R.color.cl_redformula_text_3837)));
            tv_invset_money.setText(spaBuilder_tv_invset_money);
        } else if (TextUtils.equals(invest.applyprogress, "110")) {
            tv_next_amount.setVisibility(View.GONE);
            tv_next_term.setVisibility(View.GONE);
            tv_incomemonet.setVisibility(View.VISIBLE);

            /**募集状态**/
            tv_prd_progress.setVisibility(View.VISIBLE);
            tv_prd_progress.setText("已拒绝");

            //一审（风控）拒绝--不会有这种状态返回
            /**撤销按钮**/
            btn_cancel.setVisibility(View.GONE);
            /**申请金额|募集总额**/
            tv_raise_amount.setText(getString(R.string._raise_totle_amount));
            /**还款进度|借款期限**/
            spaBuilder_tv_invset_money = (SpannableStringBuilderUtils.colorSpan
                    (getString(R.string.loan_term, invest.getTerm()), invest.getTerm(), mContext.getResources().getColor(R.color.cl_redformula_text_3837)));
            tv_invset_money.setText(spaBuilder_tv_invset_money);
        } else {
            tv_next_amount.setVisibility(View.GONE);
            tv_next_term.setVisibility(View.GONE);
            tv_incomemonet.setVisibility(View.VISIBLE);

            /**募集状态**/
            tv_prd_progress.setVisibility(View.VISIBLE);
            tv_prd_progress.setText("已拒绝");

            //二审（运营）拒绝--不会有这种状态返回
            /**撤销按钮**/
            btn_cancel.setVisibility(View.GONE);
            /**申请金额|募集总额**/
            tv_raise_amount.setText(getString(R.string._raise_totle_amount));
            /**还款进度|借款期限**/
            spaBuilder_tv_invset_money = (SpannableStringBuilderUtils.colorSpan
                    (getString(R.string.loan_term, invest.getTerm()), invest.getTerm(), mContext.getResources().getColor(R.color.cl_redformula_text_3837)));
            tv_invset_money.setText(spaBuilder_tv_invset_money);
        }
    }

    @Override
    public <T> void initializeViews(String task_name, T data) {
        // TODO Auto-generated method stub
        if (TextUtils.equals(TaskConstants.TASK_APPLYCANCEL, task_name)) {
            showToast(getString(R.string.loan_revoke_success));
            zwgt_data = BaseRequestTool2.getFormatedDateString();
            mPresenter.loadingData(TaskConstants.LOAN_LIST, getString(R.string.root_url_test) + getString(R.string.api_user_test) + TaskConstants.LOAN_LIST + "?type=" + 2,
                    Constants.EVENT_REFRESH_DATA, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_GET, getString(R.string.api_user_test) + TaskConstants.LOAN_LIST + "?type=" + 2,
                            sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data), false);
        }
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
        if (isFastDoubleClick()){
            return;
        }

        if (position == 0 || position > data.size()) return;
        Bundle bundle = new Bundle();
        //携带所有的数据过去
        bundle.putSerializable(Extras.DATA, data.get(position - 1));
        readyGo(LoanDetailActivity.class, bundle);
    }

    @Override
    public void onItemClick(Object o, int position) {
        if ((HSDialog) o == dialog_applycancel) {
            if (position != -1) {
                zwgt_data = BaseRequestTool2.getFormatedDateString();
                JSONObject map = new JSONObject();
                try {
                    map.put("uuid", uuid);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                JSONObject jsonParams = CommTool.getJSONParams(map, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);
                mPresenter.loadingData(TaskConstants.TASK_APPLYCANCEL, mContext.getString(R.string.root_url_test) + mContext.getString(R.string.api_loan_test)
                        + TaskConstants.TASK_APPLYCANCEL, Constants.EVENT_DATA, jsonParams, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_POST, mContext.getString(R.string.api_loan_test)
                        + TaskConstants.TASK_APPLYCANCEL, sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data),true);
            }
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == event.KEYCODE_BACK) {
            if (dialog_applycancel != null && dialog_applycancel.isShowing()) {
                dialog_applycancel.dismiss();
                return true;
            } else {
                return false;
            }
        }
        return false;
    }


    /**
     * 防止按钮连续点击
     */
    private static long lastClickTime;
    public static boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        if ( time - lastClickTime < 500) {
            return true;
        }
        lastClickTime = time;
        return false;
    }
}

